// pages/myArtcle/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "近一个月",
        handleWidth: 0,
        initPageX: 0,
        id: null,
        detail: []
    },
    // 触摸
    touchHandleStart(e) {
        const id = e.currentTarget.dataset.id
        if (id) {
            this.setData({
                initPageX: e.touches[0].clientX,
                id: id
            })
        }
    },
    // 移动
    touchmoveHandle(e) {
        if (!this.data.id) return false
        const maxMove = 180
        const currentPageX = e.touches[0].clientX
        const {
            handleWidth,
            initPageX
        } = this.data
        const difference = parseInt(initPageX - currentPageX)
        let moveSize = undefined
        if (difference > 0) {
            if (difference >= maxMove) {
                moveSize = maxMove
            } else {
                if (handleWidth == maxMove) {
                    moveSize = maxMove
                } else {
                    moveSize = difference
                }
            }
        } else if (difference <= 0) {
            if (handleWidth <= 0) {
                moveSize = 0
            } else {
                moveSize = maxMove - Math.abs(difference)
            }
        }
        this.setData({
            handleWidth: moveSize
        })
        // console.log(e.touches[0].clientX)
    },
    touchendHandle(e) {
        const {
            handleWidth,
            initPageX
        } = this.data
        let moveSize = undefined
        if (handleWidth >= 60 && initPageX > handleWidth) {
            moveSize = 180
        } else if (handleWidth <= 120) {
            moveSize = 0
        }
        this.setData({
            handleWidth: moveSize,
            initPageX: 0
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    changeValue(e) {
        this.setData({
            value: e.detail.value
        })
        const searchDate = new Date(e.detail.value)
        this.getArticleList({
            searchDate
        })
    },
    // 获取列表数据
    async getArticleList(params) {
        const res = await wx.cloud.callFunction({
            name: 'getList',
            data: {
                asDate: params.searchDate,
                pageSize: 0
            }
        })
        this.setData({
            detail: res.result.result.data
        })
    },
    // 删除 
    async deleteItem() {
        if (this.data.handleWidth == 0) return
        const isDel = await wx.showModal({
            title:'确认删除本条内容？',
            content:'删除后将无法恢复'
        })
        if(isDel.cancel) return this.setData({
            handleWidth: 0
        })
        wx.showLoading({
            title: '正在操作',
            mask:true
        })
        const that = this
        const res = await wx.cloud.callFunction({
            name: 'delItem',
            data: {
                id:that.data.id
            }
        })
        wx.hideLoading()
        if (res.result.result.stats.removed == 1) {
            const params = that.isValidDate()
            that.getArticleList({searchDate:params})
            that.setData({
                handleWidth: 0
            })
            wx.showToast({
                title: '删除成功',
                duration: 1000,
                icon: 'success',
            })
            return true
        }
        wx.showToast({
            title: '删除失败',
            duration: 1000,
            icon: 'error',
        })
    },
    // 修改
    ModifyItem() {
        const that = this
        const {id} = this.data
        wx.navigateTo({
            url:'/addPages/release/index',
            events:{
                updateMyArticleList:()=>{
                    const params = that.isValidDate()
                    that.getArticleList({searchDate:params})
                    that.setData({
                        handleWidth:0
                    })
                }
            },
            success(res){
                res.eventChannel.emit('getItemInfo',{id:id})
            }
        })
    },

    // 判断当前日期格式是否正确
    isValidDate(){
        const recentMonth = new Date(new Date() - 2592000000)
        let params = new Date(this.data.value)
        const isDate =  params instanceof Date && !isNaN(params.getTime())
        if(isDate) return params
        return recentMonth
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        const recentMonth = new Date(new Date() - 2592000000)
        console.log
        this.getArticleList({
            searchDate: recentMonth
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})